package cc.wanforme.fsync.config;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.Objects;

/** /client/** 的请求头验证功能
 * @author wanne
 * @date 2023-07-21
 */
public class ClientInterceptor implements HandlerInterceptor {

	private String header;
	private String value;

	public ClientInterceptor(String header, String value) {
		this.header = header;
		this.value = value;
	}

	@Override
	public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler)
			throws Exception {
		if(!StringUtils.hasText(header) || !StringUtils.hasText(value)) {
			return true;
		}

		String headVal = req.getHeader(header);
		return Objects.equals(value, headVal);
	}

}
